package com.reggie.service.impl;

import com.reggie.context.BaseContext;
import com.reggie.dto.ShoppingCartDTO;
import com.reggie.entity.Dish;
import com.reggie.entity.Setmeal;
import com.reggie.entity.ShoppingCart;
import com.reggie.mapper.DishMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.mapper.ShoppingCartMapper;
import com.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //新增购物车
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //新建一个购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //将前端传来的封装对象 拷贝到新建的购物车对象
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        //设置当前用户的id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //动态条件查询 获取购物车内的信息
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() == 1) {
            //如果购物车内有菜品  商品数量加一 执行修改操作
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {//如果没有查询到数据就执行 添加操作

            //判断当前添加到购物车的是菜品还是套餐

            if (shoppingCartDTO.getDishId() != null) {
                //如果获取到了菜品id 就是添加菜品  通过前端传的数据 获取到对应id的菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                //将获取到的菜品的名称 图片地址 价格 传给购物车对象
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            } else {
                //如果获取到的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                //将获取到的套餐的名称 图片地址 价格 传给购物车对象
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }
            //设置购物车的数量
            shoppingCart.setNumber(1);
            //购物车创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }

    }

    //查看购物车
    @Override
    public List<ShoppingCart> showShoppingCart() {
        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());

    }

    //清空购物车
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());

    }

    //删除购物车的菜品
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查询购物车中的当前选定的菜品  也就是点减号会自动获取到当前这个菜品的信息
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //判断当前这个菜品存不存在
        if(shoppingCartList!=null&&shoppingCartList.size()==1){
             shoppingCart = shoppingCartList.get(0);
             //如果存在 获取购物车中 单个菜品的数量
            Integer number = shoppingCart.getNumber();
            if(number>1){
                //当前的菜品数量如果大于一个 那就删除一个
                shoppingCart.setNumber(number-1);
                //并将删除后的菜品数量重新更新数据库
                shoppingCartMapper.updateNumberById(shoppingCart);
            }else{
                //如果菜品数量只有一个 那就直接删除
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }

        }
    }
}
